package com.atguigu.yygh.hosp.controller;

import com.atguigu.yygh.common.MD5;
import com.atguigu.yygh.common.R;
import com.atguigu.yygh.exception.MyException;
import com.atguigu.yygh.hosp.service.HospitalSetService;
import com.atguigu.yygh.model.hosp.HospitalSet;
import com.atguigu.yygh.vo.hosp.HospitalQueryVo;
import com.atguigu.yygh.vo.hosp.HospitalSetQueryVo;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Random;

@Api(description = "医院设置接口")
@RestController
@RequestMapping("/admin/hosp/hospitalSet")
public class HospitalSetController {
    @Autowired
    HospitalSetService hospitalSetService;
    @ApiOperation(value = "医院设置列表")
    @GetMapping("findAll")
    public R findAll() {
        List<HospitalSet> list = hospitalSetService.list();
        return R.ok().data("list",list);
    }
    @ApiOperation(value = "医院设置删除")
    @DeleteMapping("{id}")
    public R removeById(@PathVariable String id){
        hospitalSetService.removeById(id);
        return R.ok();
    }
    @PostMapping("/{page}/{limit}")
    public R pageList(@PathVariable long page, @PathVariable long limit,@RequestBody HospitalSetQueryVo hospitalSetQueryVo){
        Page<HospitalSet> pageParam = new Page<>(page, limit);
        return hospitalSetService.pageList(pageParam, hospitalSetQueryVo);
    }
    @PostMapping
    public R save(@RequestBody HospitalSet hospitalSet) {
        hospitalSet.setStatus(1);
        Random random=new Random();
        int anInt = random.nextInt(1000);
        hospitalSet.setSignKey(MD5.encrypt(System.currentTimeMillis()+""+anInt));
        hospitalSetService.save(hospitalSet);
        return R.ok();
    }
    @GetMapping("{id}")
    public R getById(@PathVariable String id){

        HospitalSet hospitalSet = hospitalSetService.getById(id);
        return R.ok().data("item", hospitalSet);
    }
    @PostMapping("{id}")
    public R updateById(@RequestBody HospitalSet hospitalSet){
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }
    @DeleteMapping("batchRemove")
    public R batchRemoveHospitalSet(@RequestBody List<Long> idList) {
        hospitalSetService.removeByIds(idList);
        return R.ok();
    }
    @PutMapping("lockHospitalSet/{id}/{status}")
    public R lockHospitalSet(@PathVariable Long id,
                                  @PathVariable Integer status) {
        //根据id查询医院设置信息
        HospitalSet hospitalSet = hospitalSetService.getById(id);
        //设置状态
        hospitalSet.setStatus(status);
        //调用方法
        hospitalSetService.updateById(hospitalSet);
        return R.ok();
    }
}
